package models

import (
	"time"

	"github.com/google/uuid"
)

// Flow 工作流实体
type Flow struct {
	ID          string    `json:"id" gorm:"primaryKey;type:text"`
	Name        string    `json:"name" gorm:"not null"`
	Description string    `json:"description"`
	Branch      string    `json:"branch" gorm:"not null;index:idx_flows_branch_repo"`   // 监听的分支
	RepoURL     string    `json:"repo_url" gorm:"not null;index:idx_flows_branch_repo"` // 仓库地址
	GitUsername string    `json:"git_username"`                                          // Git用户名（可选）
	GitToken    string    `json:"git_token"`                                             // Git访问令牌（可选）
	CreatedAt   time.Time `json:"created_at" gorm:"not null"`
	UpdatedAt   time.Time `json:"updated_at" gorm:"not null"`
}

// Node 工作流步骤实体
type Node struct {
	ID          string    `json:"id" gorm:"primaryKey;type:text"`
	FlowID      string    `json:"flow_id" gorm:"not null;type:text"`
	Name        string    `json:"name" gorm:"not null"`
	Description string    `json:"description"`
	Order       int       `json:"order" gorm:"not null"` // 执行顺序
	CreatedAt   time.Time `json:"created_at" gorm:"not null"`
	UpdatedAt   time.Time `json:"updated_at" gorm:"not null"`

	// 关联关系
	Flow Flow `json:"-" gorm:"foreignKey:FlowID;references:ID"`
}

// Action Node中要执行的动作
type Action struct {
	ID        string    `json:"id" gorm:"primaryKey;type:text"`
	NodeID    string    `json:"node_id" gorm:"not null;type:text"`
	Name      string    `json:"name" gorm:"not null"`
	Type      string    `json:"type" gorm:"not null"`    // shell, python
	Content   string    `json:"content" gorm:"not null"` // 脚本内容
	Order     int       `json:"order" gorm:"not null"`   // 执行顺序
	CreatedAt time.Time `json:"created_at" gorm:"not null"`
	UpdatedAt time.Time `json:"updated_at" gorm:"not null"`

	// 关联关系
	Node Node `json:"-" gorm:"foreignKey:NodeID;references:ID"`
}

// FlowExecution Flow执行记录
type FlowExecution struct {
	ID        string     `json:"id" gorm:"primaryKey;type:text"`
	FlowID    string     `json:"flow_id" gorm:"not null;type:text"`
	Status    string     `json:"status" gorm:"not null"` // running, success, failed
	StartTime time.Time  `json:"start_time" gorm:"not null"`
	EndTime   *time.Time `json:"end_time"`
	Error     string     `json:"error"`
	CommitID  string     `json:"commit_id"`
	Branch    string     `json:"branch"`

	// 关联关系
	Flow Flow `json:"-" gorm:"foreignKey:FlowID;references:ID"`
}

// NodeExecution Node执行记录
type NodeExecution struct {
	ID              string     `json:"id" gorm:"primaryKey;type:text"`
	FlowExecutionID string     `json:"flow_execution_id" gorm:"not null;type:text"`
	NodeID          string     `json:"node_id" gorm:"not null;type:text"`
	Status          string     `json:"status" gorm:"not null"` // running, success, failed
	StartTime       time.Time  `json:"start_time" gorm:"not null"`
	EndTime         *time.Time `json:"end_time"`
	Error           string     `json:"error"`
	Output          string     `json:"output"`

	// 关联关系
	FlowExecution FlowExecution `json:"-" gorm:"foreignKey:FlowExecutionID;references:ID"`
	Node          Node          `json:"-" gorm:"foreignKey:NodeID;references:ID"`
}

// ActionExecution Action执行记录
type ActionExecution struct {
	ID              string     `json:"id" gorm:"primaryKey;type:text"`
	NodeExecutionID string     `json:"node_execution_id" gorm:"not null;type:text"`
	ActionID        string     `json:"action_id" gorm:"not null;type:text"`
	Status          string     `json:"status" gorm:"not null"` // running, success, failed
	StartTime       time.Time  `json:"start_time" gorm:"not null"`
	EndTime         *time.Time `json:"end_time"`
	Error           string     `json:"error"`
	Output          string     `json:"output"`

	// 关联关系
	NodeExecution NodeExecution `json:"-" gorm:"foreignKey:NodeExecutionID;references:ID"`
	Action        Action        `json:"-" gorm:"foreignKey:ActionID;references:ID"`
}

// NotificationConfig 通知配置
type NotificationConfig struct {
	ID        string    `json:"id" gorm:"primaryKey;type:text"`
	Name      string    `json:"name" gorm:"not null"`
	URL       string    `json:"url" gorm:"not null"`
	Enabled   bool      `json:"enabled" gorm:"not null;default:true"`
	CreatedAt time.Time `json:"created_at" gorm:"not null"`
	UpdatedAt time.Time `json:"updated_at" gorm:"not null"`
}

// NewID 生成新的UUID
func NewID() string {
	return uuid.New().String()
}
